﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

/*进程（日志）守护工具的数据操作基础类和接口 */

namespace NewProcessGuarder.core
{
    [Serializable]
    public class Guarders : ObservableCollection<LogGuarder>
    {
        private string guardsDataFilePath;

        public string GuardsDataFilePath
        {
            private set
            {
                guardsDataFilePath = value;
            }
            get
            {
                return guardsDataFilePath;
            }
        }

        private static string directoryPath = System.AppDomain.CurrentDomain.BaseDirectory;
        public static Guarders GetSingleGuarders(string ConfigFileLocationKey)
        {
            if (string.IsNullOrEmpty(ConfigFileLocationKey))
            {
                return new Guarders();
            }
            else
            {
                return new Guarders(ConfigFileLocationKey).LoadGuardsData();
            }
        }

        private Guarders() {
            GuardsDataFilePath = Path.Combine(directoryPath, "guarder.dat");
        }

        private Guarders(string datPath)
        {
            this.GuardsDataFilePath = Path.Combine(directoryPath, datPath);
        }

        internal new void Add(LogGuarder obj)
        {
            if (!this.Contains(obj))
                base.Add(obj);
        }

        internal new void Remove(LogGuarder obj)
        {
            if (this.Contains(obj))
                base.Remove(obj);

        }

        internal Guarders LoadGuardsData()
        {
            try
            {
                if (File.Exists(GuardsDataFilePath))
                {
                    IFormatter formatter;
                    using (Stream stream = new FileStream(GuardsDataFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        formatter = new BinaryFormatter();
                        Guarders guarders = (Guarders)formatter.Deserialize(stream);
                        guarders.GuardsDataFilePath = GuardsDataFilePath;
                        stream.Close();
                        return guarders;
                    }
                    //XmlSerializer formatter = new XmlSerializer(this.GetType());
                    
                    
                }
                else return new Guarders();
            }
            catch (Exception )
            {
                File.Delete(GuardsDataFilePath);
                return null;
            }
        }

        internal bool SaveGuardsData()
        {
            try
            {
                if (File.Exists(GuardsDataFilePath)) File.Delete(GuardsDataFilePath);
                IFormatter formatter;
                using (Stream stream = new FileStream(GuardsDataFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    formatter = new BinaryFormatter();
                    formatter.Serialize(stream, this);
                    stream.Close();
                }
                
                //XmlSerializer formatter = new XmlSerializer(this.GetType());
                
                return true;
            }
            catch (IOException )
            {
                //string msg = ex.Message;
                return false;
            }
        }
    }
}
